package com.yourenbang.lingyun.transaction.mq;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.yourenbang.lingyun.constants.enums.mq.MqEnum;
import com.yourenbang.lingyun.transaction.config.MqConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class ConsumerClient {

    @Autowired
    private MqConfig mqConfig;
    @Autowired
    private TradeLakalaMessageListener tradeLakalaMessageListener;
    @Autowired
    private FundMessageListener fundMessageListener;

    @Bean(initMethod = "start", destroyMethod = "shutdown")
    public ConsumerBean buildConsumer() {
        ConsumerBean consumerBean = new ConsumerBean();
        //配置文件
        Properties properties = mqConfig.getMqPropertie();
        //将消费者线程数固定为20个 20为默认值
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums, "30");
        properties.put(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
        consumerBean.setProperties(properties);

        Map<Subscription, MessageListener> subScriptionTable = new HashMap<>();

        //交易-拉卡拉（交易数据同步使用：蓝牙，扫码，电签，D0提款）
        Subscription tradeLakalaSubscription = new Subscription();
        tradeLakalaSubscription.setTopic(MqEnum.MqTopicEnum.topic_trade_lakala.name());
        tradeLakalaSubscription.setExpression(MqEnum.MqTagEnum.tag_trade_lakala.name());

        //商户-考拉快收（商户数据同步使用：通道商户，押金，达标，激活返现）
        Subscription merchantKlksSubscription = new Subscription();
        merchantKlksSubscription.setTopic(MqEnum.MqTopicEnum.topic_merchant_klks.name());
        merchantKlksSubscription.setExpression(MqEnum.MqTagEnum.tag_merchant_klks.name());

        //资金账户
        Subscription fundSubscription = new Subscription();
        fundSubscription.setTopic(MqEnum.MqTopicEnum.topic_fund.name());
        fundSubscription.setExpression(MqEnum.MqTagEnum.tag_fund.name());

        subScriptionTable.put(tradeLakalaSubscription, tradeLakalaMessageListener);
        subScriptionTable.put(fundSubscription, fundMessageListener);
        consumerBean.setSubscriptionTable(subScriptionTable);

        return consumerBean;
    }

}
